##########################################################################################
###  Replication code for:
###  "Do Primaries Improve Electoral Performance? Clientelism and Intra-Party Conflict in
###  Ghana," American Journal of Political Science
###
###  IchinoNathan_AJPS_primaries_matching.R
###
###  Nahomi Ichino and Noah Nathan
###  Department of Government, Harvard University
###  June 2012 
###
###  Note: It is *not* necessary to run this code before doing the replication analysis in
###  IchinoNathan_AJPS_primaries_analysis.R.  The matching output produced by this code is
###  saved as matchingoutput.Rdata.   
###
###  Calls:
###  primariesdata_NDC.csv
###  primariesdata_NPP.csv
###   
###  Produces:
###  matchingoutput.Rdata
###
###  We use:
###  Matching (Version 4.8-0, Build Date: 2012/01/19)
###  See http://sekhon.berkeley.edu/matching for additional documentation.
###   Jasjeet S. Sekhon. 2011. ``Multivariate and Propensity Score Matching
###   Software with Automated Balance Optimization: The Matching package for R.''
###   Journal of Statistical Software, 42(7): 1-52.
##########################################################################################


library(Matching)
ev<-c(FALSE,FALSE,TRUE,TRUE,TRUE)
ev.ndc<-c(FALSE,FALSE,TRUE,TRUE,TRUE)

#####################
## NDC only (ATE)

data2 <- read.csv("primariesdata_NDC.csv") 
set.seed(20110111)
x<-cbind(data2$pres_t_1, data2$ethfrac, data2$year2008, data2$holds1, data2$incumbent)
genout.2.ATE<-GenMatch(Tr=data2$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATE", M=1, pop.size=1000, max.generations=30, wait.generations=10)

####################
## NDC only (ATT)

data2 <- read.csv("primariesdata_NDC.csv") 
set.seed(20110111)
x<-cbind(data2$pres_t_1, data2$ethfrac, data2$year2008, data2$holds1, data2$incumbent)
genout.2<-GenMatch(Tr=data2$prim_elec, X=x, BalanceMatrix=x, exact=ev.ndc, estimand="ATT", M=1, pop.size=1000, max.generations=30, wait.generations=10)

####################
## NPP only (ATE)

data1 <- read.csv("primariesdata_NPP.csv") 
set.seed(20110111)
x<-cbind(data1$pres_t_1, data1$ethfrac, data1$year2008, data1$holds1, data1$incumbent)
genout.1.ATE<-GenMatch(Tr=data1$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATE", M=1, pop.size=1000, max.generations=30, wait.generations=10)

####################
## NPP only (ATT)

data1 <- read.csv("primariesdata_NPP.csv") 
set.seed(20110111)
x<-cbind(data1$pres_t_1, data1$ethfrac, data1$year2008, data1$holds1, data1$incumbent)
genout.1<-GenMatch(Tr=data1$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATT", M=1, pop.size=1000, max.generations=30, wait.generations=10)

###########################
### Stronghold NPP (ATE)

data.nnpp <- read.csv("primariesdata_NPP.csv") 
data.nnpp <- subset(data.nnpp, data.nnpp$pres_t_1>.6)
set.seed(20110111)
x<-cbind(data.nnpp$pres_t_1, data.nnpp$ethfrac, data.nnpp$year2008, data.nnpp$holds1, data.nnpp$incumbent)
genout.nnpp.ATE<-GenMatch(Tr=data.nnpp$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATE", M=1, pop.size=1000, max.generations=30, wait.generations=10)

##############################
### Stronghold NPP (ATT)

data.nnpp <- read.csv("primariesdata_NPP.csv") 
data.nnpp <- subset(data.nnpp, data.nnpp$pres_t_1>.6)
set.seed(20110111)
x<-cbind(data.nnpp$pres_t_1, data.nnpp$ethfrac, data.nnpp$year2008, data.nnpp$holds1, data.nnpp$incumbent)
genout.nnpp<-GenMatch(Tr=data.nnpp$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATT", M=1, pop.size=1000, max.generations=30, wait.generations=10)


###############################################################
### Competitive NPP only (ATE)

data.cnpp <- read.csv("primariesdata_NPP.csv") 
data.cnpp <- subset(data.cnpp, data.cnpp$pres_t_1<.6)
data.cnpp <- subset(data.cnpp, data.cnpp$pres_t_1_other<.6)
set.seed(20110111)
x<-cbind(data.cnpp$pres_t_1, data.cnpp$ethfrac, data.cnpp$year2008, data.cnpp$holds1, data.cnpp$incumbent)
genout.cnpp.ATE<-GenMatch(Tr=data.cnpp$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATE", M=1, pop.size=1000, max.generations=30, wait.generations=10)

##############################
### Competitive NPP only (ATT)

data.cnpp <- read.csv("primariesdata_NPP.csv") 
data.cnpp <- subset(data.cnpp, data.cnpp$pres_t_1<.6)
data.cnpp <- subset(data.cnpp, data.cnpp$pres_t_1_other<.6)
set.seed(20110111)
x<-cbind(data.cnpp$pres_t_1, data.cnpp$ethfrac, data.cnpp$year2008, data.cnpp$holds1, data.cnpp$incumbent)
genout.cnpp<-GenMatch(Tr=data.cnpp$prim_elec, X=x, BalanceMatrix=x, exact=ev, estimand="ATT", M=1, pop.size=1000, max.generations=30, wait.generations=10)

#####################################################################
### save workspace

save(data1, data2, data.cnpp, data.nnpp, genout.1, genout.2, genout.cnpp, genout.nnpp, genout.1.ATE, genout.2.ATE, genout.cnpp.ATE, genout.nnpp.ATE, file="matchingoutput1.Rdata")
















